import { Body, Controller, Get, HttpStatus, Post, Query } from '@nestjs/common';
import { UserService } from './user.service';
import { MessagePattern } from '@nestjs/microservices';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { RegisterUserDto } from './dto/register-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { RequireLogin } from '@app/auth/utils/guard';
import { PublicVoError, PublicVoSuccess } from '@app/utils/publicVo';
import { UserListVo } from './vo/user-list.vo';
import { UserVo } from './vo/user.vo';

@ApiTags('用户系统-用户中心')
@Controller()
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * TCP 模块
   */
  @MessagePattern('post-register')
  async postRegisterTCP(data: any) {
    return await this.userService.register(data.body);
  }

  @MessagePattern('get-register/email')
  async postRegisterEmailTCP(data: any) {
    return await this.userService.registerEmail(data.query.email);
  }

  @MessagePattern('post-login')
  async postLoginTCP(data: any) {
    return await this.userService.login(data.body);
  }

  @MessagePattern('get-login/svg')
  async getLoginSvgTCP() {
    return await this.userService.loginSvg();
  }
  @MessagePattern('get-list')
  async getListTCP() {
    return await this.userService.list();
  }
  /**
   * HTTP 模块
   */

  @Get('register/email')
  @ApiOperation({
    summary: '注册邮箱验证码',
    description: '注册邮箱验证码接口',
  })
  @ApiQuery({
    name: 'email',
    type: String,
    description: '邮箱地址',
    required: true,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async postRegisterEmail(@Query('email') email: string) {
    return await this.userService.registerEmail(email);
  }

  @Post('register')
  @ApiOperation({ summary: '用户注册', description: '用户注册接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async postRegister(@Body() registerUser: RegisterUserDto) {
    return await this.userService.register(registerUser);
  }

  @Get('login/svg')
  @ApiOperation({
    summary: '登录图形验证码',
    description: '登录图形验证码接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async getLoginSvg() {
    return await this.userService.loginSvg();
  }

  @Post('login')
  @ApiOperation({ summary: '用户登录', description: '用户登录接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserListVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async postLogin(@Body() loginUserDto: LoginUserDto) {
    return await this.userService.login(loginUserDto);
  }

  @Get('list')
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({ summary: '用户列表', description: '用户列表接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async getList() {
    return await this.userService.list();
  }
}
